<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="姓名" clearable></el-input>
                    <el-input v-model="search.mobile" placeholder="手机号码" clearable></el-input>
                    <el-input v-model="search.agentName" placeholder="所属机构" clearable></el-input>
                    <el-select v-model="search.gender" placeholder="性别" clearable>
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="2"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="启用" :value="1"></el-option>
                        <el-option label="禁用" :value="0"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="table_add" v-auth="'users-user-add'">新增</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.user.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="账号" prop="account" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.authAccount ? scope.row.authAccount.account : ''  }}
                        </template>
                    </el-table-column>
                    <el-table-column label="姓名" prop="name" width="150" align="center"></el-table-column>
                    <el-table-column label="所属机构" prop="agent" width="160" align="center">
                        <template #default="scope">
                            {{ scope.row.agent ? scope.row.agent.name : ''  }}
                        </template>
                    </el-table-column>
                    <el-table-column label="30课时" prop="classHour30" width="130" align="center"></el-table-column>
                    <el-table-column label="60课时" prop="classHour60" width="130" align="center"></el-table-column>
                    <el-table-column label="性别" prop="gender" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.gender ? scope.row.gender.name : ''  }}
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" prop="mobile" width="130" align="center"></el-table-column>
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="200">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row)">查看</el-button>
                            <el-divider direction="vertical" v-if="$ROLE('users-user-edit')"></el-divider>
                            <el-button type="text" size="small" @click="table_edit(scope.row)" v-auth="'users-user-edit'">编辑</el-button>
                            <el-divider direction="vertical" v-if="$ROLE('users-user-status') || $ROLE('users-user-recharge') || $ROLE('users-user-refund')"></el-divider>
                            <el-dropdown v-if="$ROLE('users-user-status') || $ROLE('users-user-recharge') || $ROLE('users-user-refund')">
                                <el-button type="text" size="small">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item v-if="$ROLE('users-user-recharge')" @click="table_recharge(scope.row)">充值</el-dropdown-item>
                                        <el-dropdown-item divided v-if="$ROLE('users-user-refund')" @click="table_refund(scope.row)">退费</el-dropdown-item>
                                        <el-popconfirm v-if="$ROLE('users-user-status') && scope.row.status.value == 0" :title="'确认是否要启用学员: '+scope.row.name+'?'" @confirm="table_status(scope.row, 1)">
                                            <template #reference>
                                                <el-dropdown-item divided>启用</el-dropdown-item>
                                            </template>
                                        </el-popconfirm>
                                        <el-popconfirm v-if="$ROLE('users-user-status') && scope.row.status.value == 1" :title="'确认是否要禁用学员: '+scope.row.name+'?'" @confirm="table_status(scope.row, 0)">
                                            <template #reference>
                                                <el-dropdown-item divided>禁用</el-dropdown-item>
                                            </template>
                                        </el-popconfirm>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" :rolelist="rolelist" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

    <recharge-dialog v-if="dialog.recharge" ref="rechargeDialog" @closed="dialog.recharge=false" @success="handleSuccess"></recharge-dialog>

    <refund-dialog v-if="dialog.refund" ref="refundDialog" @closed="dialog.refund=false" @success="handleSuccess"></refund-dialog>
</template>

<script>
import saveDialog from './save'
import detailDialog from './detail'
import rechargeDialog from './components/recharge'
import refundDialog from './components/refund'
export default {
    name: 'user',
    components: {
        saveDialog,
        detailDialog,
        rechargeDialog,
        refundDialog
    },
    data() {
        return {
            dialog: {
                save: false,
                detail: false,
                recharge: false,
                refund: false,
            },
            selection: [],
            search: {
                name: null,
                mobile: null,
                status: null,
            },
        }
    },
    mounted() {
        
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        // 增加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        // 编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id, 'first')
			})
        },
        //状态变更
        async table_status(row, status) {
            await this.$API.user.updateStatus.put({ id: row.id, status: status }).then(() => {
                this.$notify.success("更新成功")
                this.$refs.table.refresh()
            })
        },
        // 充值
        table_recharge(row) {
            this.dialog.recharge = true
            this.$nextTick(() => {
                this.$refs.rechargeDialog.open('学员【'+row.name+'】课时充值').setData(row)
            })
        },
        // 退费
        table_refund(row) {
            this.dialog.refund = true
            this.$nextTick(() => {
                this.$refs.refundDialog.open('学员【'+row.name+'】课时退费').setData(row)
            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
